﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace zserialize
{
    public class FieldDictionary: Dictionary<string, object>
    {
        public string className;
        public object userData;
        public object selfTable;
        
        public void Set(string fieldName, object value)
        {
            if (ContainsKey(fieldName))
            {
                base[fieldName] = value;
            }
            else
            {
                this.Add(fieldName, value);
            }
        }

        public object Get(string fieldName)
        {
            bool hasKey = false;
            return Get(fieldName, out hasKey);
        }

        public object Get(string fieldName, out bool hasKey)
        {
            object value = null;
            hasKey = TryGetValue(fieldName, out value);
            return value;
        }

    //    public object this[string sKey]
    //    {
    //        get
    //        {
    //            return Get(sKey);
    //        }
    //
    //        set
    //        {
    //            base[sKey] = value;
    //        }
    //    }


        public override string ToString()
        {
            string s = base.ToString();

            string dicLog = "";
            Dictionary<string, object>.Enumerator e = this.GetEnumerator();
            while (e.MoveNext())
            {
                dicLog += e.Current.Key + ":" + e.Current.Value + "\n";
            }

            s += "\n count:" + this.Count;
            s += "\n" + dicLog;
            return s;
        }
    }
}